//var wx = 0;
//var wy = 0;

$(document).ready(function(){
	/*console.log("document ready ...");*/
	

	window.console = window.console || (function () {
	    var c = {}; c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile
	    = c.clear = c.exception = c.trace = c.assert = function () { };
	    return c;
	})();
	// layout north 面板收缩时添加标题
	$(".layout-expand-north .panel-header .panel-title").html("快速查询");
	
//	$('body').layout('panel','north').panel({
//		onCollapse:function(){
//			console.log('onCollapse redraw');
//			// layout north 面板收缩时添加标题
//			$(".layout-expand-north .panel-header .panel-title").html("快速查询");
//		}
////		,onExpand:function(){
////			console.log('onExpand redraw');
////		},
////		onResize:function(width,height){
////			console.log('onResize redraw');
////		}
//	});
	
	// 记录窗口初始大小
	wx = $(window).width();
	wy = $(window).height();
	
	// 监听 浏览器窗口 变动 事件,对子layout进行重绘
	$(window).resize(function(){
		console.log('window resize');
		//winredraw();
	});
});


//窗口重绘方法
var winredraw = function(){
	// 获取当前窗口的高度
	var x = $(window).width();
	var y = $(window).height();
	
	console.log('window resize ok, x-wx=[' + x + ',' + wx + '],y-wy=[' + y + ',' + wy +']');
	// 当窗口大小变动时，重绘子layout
	$('#gridLayout').layout('resize');
	console.log('gridLayout resize ok');

	// 储存当前窗口高度
	wx = x;
	wy = y;
};

//选中行检查
var isSelected = function(row){
 if (!row){
 	//$.messager.alert('提示', '请选择一条记录！', 'error');
 	$.slideMsg("请选择一条记录！");
 	return false;
 }
 return true;
};

// 弹出Dialog
var loadDialog = function(grid, tree, name, url, width, height) {
	if(!width){
		width = 920;
	}
	if(!height){
		height = $(window).height() * 0.9;
	}
	// 弹出窗口
 	var dialog = parent.sy.modalDialog({
 		iconCls: 'icon-save',
        cls:'theme-panel-orange',
 		title : name,
 		url : url,
 		buttons : [ {
 			text : '&nbsp;&nbsp;确&nbsp;定&nbsp;&nbsp;',
 			handler : function() {
 				dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, tree, parent.$);
 			}
 		},
  		{
 			text : '&nbsp;&nbsp;取&nbsp;消&nbsp;&nbsp;',
 			handler : function() {
 				dialog.dialog('destroy');
 			}
 		}  ]
 	}, width, height);
};

//查看Dialog
var loadViewDialog = function(name, url, param, width, height) {
	if(!width){
		width = 920;
	}
	if(!height){
		height = $(window).height() * 0.9;
	}
	// 弹出窗口
 	var dialog = parent.sy.modalDialog({
 		iconCls: 'icon-save',
        cls:'theme-panel-orange',
 		title : name,
 		url : url,
 		buttons : [ 
  		{
 			text : '&nbsp;&nbsp;关&nbsp;闭&nbsp;&nbsp;',
 			handler : function() {
 				if(name=="查看公告"){
 					$("#announceInfo").datagrid('reload');
 				}
 				dialog.dialog('destroy');
 			}
 		}  ]
 	}, width, height);
};
//关闭Dialog
var closeDialog=function(){
	
	parent.dialog('destroy');
	
}

// 导入数据Dialog
var importDialog = function(grid, name, url) {
	// 弹出窗口
 	var dialog = parent.sy.modalDialog({
 		title : name,
 		url : url,
 		buttons : [ {
 			text : '确定',
 			handler : function() {
 				dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
 			}
 		} ]
 	}, 300, 180);
};

// 确认删除
var confirmDel = function($grid, objectCode, deleted){
	$.messager.confirm('请确认', '您要删除当前所选数据？', function(o) {
		if (o) {
			var json2 = JSON.stringify(deleted);
            console.log('delete数据' + json2);
            var $jq = $(this);
            $.syncPost('/grid/delete/' + objectCode, {rows: json2},
                    function (result, status) {
                        if (result.success) {
                        	$.slideMsg("删除成功！");
                        	if($grid.context.URL.indexOf('/single_tree') != -1){
                        		$grid.treegrid('load');
                        	} else {
                        		$grid.datagrid('load');
                        	}
                        } else {
                        	$.messager.alert("操作错误", result.msg ,'error');
                        }
                    });
		}
	});
};

//扩展日期插件 只显示年月
var yearMonth = function($date){
	$date.datebox({
        onShowPanel: function () {//显示日趋选择对象后再触发弹出月份层的事件，初始化时没有生成月份层
            span.trigger('click'); //触发click事件弹出月份层
            if (!tds)
                setTimeout(function () { //延时触发获取月份对象，因为上面的事件触发和对象生成有时间间隔
                    tds = p.find('div.calendar-menu-month-inner td');
                    tds.click(function (e) {
                        e.stopPropagation(); //禁止冒泡执行easyui给月份绑定的事件
                        var year = /\d{4}/.exec(span.html())[0] //得到年份
                            ,
                            month = parseInt($(this).attr('abbr'), 10); //月份
                        $date.datebox('hidePanel') //隐藏日期对象
                            .datebox('setValue', year + '-' + month); //设置日期的值
                    });
                }, 0);
        },
        parser: function (s) {//配置parser，返回选择的日期
            if (!s) return new Date();
            var arr = s.split('-');
            return new Date(parseInt(arr[0], 10), parseInt(arr[1], 10) - 1, 1);
        },
        formatter: function (d) { 
        	var month = d.getMonth() + 1;
        	if(month >0 && month < 10){
        		month = '0' + month;
        	}
        	return d.getFullYear() + '-' + month; 
        }//配置formatter，只返回年月
    });
    var p = $date.datebox('panel'), //日期选择对象
        tds = false, //日期选择对象中月份
        span = p.find('span.calendar-text'); //显示月份层的触发控件
};